package com.mspbots.api.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.api.service.TenantUserTimeEntryService;
import com.mspbots.cw.request.TenantTimeEntryDTO;
import com.mspbots.dto.page.IPage;
import com.mspbots.dto.page.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * com.mspbots.api.service.impl.TenantUserTimeEntryImpl
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/6/19
 */
@Deprecated
@Service
public class TenantUserTimeEntryImpl extends BaseGenericService implements TenantUserTimeEntryService {

    @Override
    public List<TenantTimeEntryDTO> addBatch(List<TenantTimeEntryDTO> timeEntries) {
        Mono<JsonNode> resultMono = this.apiClient.post("/tenant/user-time-entry/add-batch", timeEntries);
        JsonNode result = resultMono.block();
        timeEntries = this.objectMapper.convertValue(result, new TypeReference<List<TenantTimeEntryDTO>>() {
        });
        return timeEntries;
    }

    @Override
    public List<TenantTimeEntryDTO> findForRule(Map<String, Object> params) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/user-time-entry/analysis-list", params);
        try {
            JsonNode result = resultMono.block();
            return this.objectMapper.convertValue(result, new TypeReference<List<TenantTimeEntryDTO>>() {
            });
        } catch (Exception e) {

        }
        return new ArrayList<TenantTimeEntryDTO>();

    }

    @Override
    public IPage<TenantTimeEntryDTO> selectPage(Map<String, Object> params) {

        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/user-time-entry/page", params);

        JsonNode result = resultMono.block();
        Page<TenantTimeEntryDTO> timeEntryPage = this.objectMapper.convertValue(result, new TypeReference<Page<TenantTimeEntryDTO>>() {
        });
        List<TenantTimeEntryDTO> dtos = timeEntryPage.getRecords().stream().peek(tenantTimeEntryDTO -> {
            Mono<JsonNode> tenantMono = this.apiClient.get("/tenant/" + tenantTimeEntryDTO.getTenantId() + "/info");
            JsonNode jsonNode = tenantMono.block();
            if (!ObjectUtils.isEmpty(jsonNode)) {
                tenantTimeEntryDTO.setTenantName(jsonNode.findValue("name").asText());
            }
        }).collect(Collectors.toList());

        timeEntryPage.setRecords(dtos);

        return timeEntryPage;
    }

    @Override
    public List<TenantTimeEntryDTO> listByTenantUserId(Map<String, Object> params) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/user-time-entry/tenant-user-list", params);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, new TypeReference<List<TenantTimeEntryDTO>>() {
        });
    }
}
